#include "cool_label.h"

CoolLabel::CoolLabel( Widget* _parent, Engine* _engine,
                      QString _text, int _x, int _y, int _w, int _h,
                      TextAlignment _textAlignment )
                          :Label( _parent, _engine, _text, _x, _y, _w, _h, _textAlignment )
{
    textX = 0;
    g.AddSimpleProperty( "tx", textX );
    g.AddInterpolator( showAnime.GetTime(), 0, showAnime.GetDuration(), "tx", 0, w - 10 );
}

CoolLabel::~CoolLabel( void )
{
    ;
}

void CoolLabel::doShow()
{
    paintEnabled = true;
    showAnime.Stop();
    showAnime.Play();
}

void CoolLabel::doHide()
{
    showAnime.Reverse(true);
    showAnime.Play();
}

void CoolLabel::OnEnd( Animation *a )
{
    if(a->IsReversed())
    {   // hiding
        visible = false;
        paintEnabled = false;
        Hidden();
    }else
    {   // showing
        visible = true;
        Showed();
    }
}

void CoolLabel::Paint( QPainter &widget_painter )
{
    pixmap->fill(QColor(255, 255, 255, 0));
    QPainter painter(pixmap);

    // paints the background
    backColor.setRed(backBrushRed);
    backColor.setGreen(backBrushGreen);
    backColor.setBlue(backBrushBlue);
    backColor.setAlpha(backBrushAlpha);
    backBrush.setColor(backColor);
    painter.setBrush(backBrush);
    painter.setPen(QPen(QColor(0, 0, 0, 0)));
    painter.drawRect(-1, -1, w + 1, h + 1);

    // paints the text
    painter.setFont(font);
    color.setRed(penRed);
    color.setGreen(penGreen);
    color.setBlue(penBlue);
    color.setAlpha(penAlpha);
    pen->setColor(color);
    pen->setWidth(penWidth);
    painter.setPen(*pen);

    // calculates the text x and y position according to the chosen alignment

    int textY = 0;
    switch(vertAlignment)
    {
    case Top:
        textY = textHeight;
        break;
    case Middle:
        textY = h - (h - textHeight)/2;
        break;
    case Bottom:
        textY = h;
        break;
    }
    painter.drawText(w - textX, textY, text);

    if(underlined)
    {
        painter.drawLine(w - textX, textY + 2, textX + textWidth, textY + 2);
    }

    widget_painter.drawPixmap(x, y, *pixmap, 0, 0, w, h);
}
